package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeviceDataServiceImpl implements DeviceDataService {
    @Resource
    private DeviceDataMapper deviceDataMapper;

    @Override
    public PageResponse<DeviceDataVo> getDeviceDataPage(DeviceDataPageReqDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Long startTime = dto.getStartTime();
        if (startTime != null) {
            dto.setStartDate(LocalDateTimeUtil.of(startTime));
        }
        Long endTime = dto.getEndTime();
        if (endTime != null) {
            dto.setEndDate(LocalDateTimeUtil.of(endTime));
        }
        Page<DeviceDataVo> page = deviceDataMapper.selectDeviceByConditions(dto);
        return PageResponse.of(page, DeviceDataVo.class);
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {
        List<DeviceDataGraphVo> list = deviceDataMapper.selectDeviceDataListByDay(iotId, functionId, LocalDateTimeUtil.of(startTime), LocalDateTimeUtil.of(endTime));
        Map<String, Double> map = list.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        List<DeviceDataGraphVo> deviceDataGraphVos = DeviceDataGraphVo.dayInstance(LocalDateTimeUtil.of(startTime));
        // 补 全 整 点 缺 失 数 据
        for (DeviceDataGraphVo deviceDataGraphVo : deviceDataGraphVos) {
            Double val = map.get(deviceDataGraphVo.getDateTime()) == null ? 0.0 : map.get(deviceDataGraphVo.getDateTime());
            deviceDataGraphVo.setDataValue(val);
        }
        return deviceDataGraphVos;
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        List<DeviceDataGraphVo> list = deviceDataMapper.selectDeviceDataListByWeek(iotId, functionId, LocalDateTimeUtil.of(startTime), LocalDateTimeUtil.of(endTime));
        Map<String, Double> map = list.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        List<DeviceDataGraphVo> deviceDataGraphVos = DeviceDataGraphVo.weekInstance(LocalDateTimeUtil.of(startTime));
        for (DeviceDataGraphVo deviceDataGraphVo : deviceDataGraphVos) {
            Double val = map.get(deviceDataGraphVo.getDateTime()) == null ? 0.0 : map.get(deviceDataGraphVo.getDateTime());
            deviceDataGraphVo.setDataValue(val);
        }
        return deviceDataGraphVos;
    }
}
